package HashMap;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.util.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SearchPanel extends JPanel 
{
	private JTextField textField;
	private JButton btnSelect;
	private JScrollPane scrollPane;
	private JTextArea textArea;

	/**
	 * Create the panel.
	 */
	public SearchPanel() 
	{
		setLayout(null);
		add(getTextField());
		add(getBtnSelect());
		add(getScrollPane());
		
		textArea.removeAll();
		
		Iterator<String> key = MapFrame.map.keySet().iterator();
		Iterator<Data> values = MapFrame.map.values().iterator();
		
		while (key.hasNext()) 
		{
			String id = key.next();
			Data d = values.next();
			
			textArea.append(d.getData()+"\n");
		}
		

	}
	

	private JTextField getTextField() 
	{
		if (textField == null) 
		{
			textField = new JTextField();
			textField.setBounds(12, 10, 315, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnSelect() 
	{
		if (btnSelect == null) 
		{
			btnSelect = new JButton("Select");
			btnSelect.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String sel_Con = textField.getText();
					
					textArea.setText("");;
					Iterator<String> key = MapFrame.map.keySet().iterator();
					Iterator<Data> values = MapFrame.map.values().iterator();
					
					while (key.hasNext()) 
					{
						String id = key.next();
						System.out.println(id);
						Data d = values.next();
						
						if (id.equals(sel_Con)) 
						{
							textArea.append(d.getData()+"\n");
						}
						else if (d.getName().contains(sel_Con)) 
						{
							textArea.append(d.getData()+"\n");
						}
						else if (d.getBirthday().contains(sel_Con)) 
						{
							textArea.append(d.getData()+"\n");
						}
						else if (d.getAddress().contains(sel_Con)) 
						{
							textArea.append(d.getData()+"\n");
						}
						/*else 
						{
							textArea.append("Nothing EXIST");
						}*/
					}
					
					//trim() = 공백을 모두 지워라
					/*if (textArea.getText().trim().equals("")) 
					{
						textArea.append("Nothing EXIST");
					}*/
					if (textArea.getText().equals("")) 
					{
						textArea.append("Nothing EXIST");
					}
				}
			});
			btnSelect.setBounds(341, 9, 97, 23);
		}
		return btnSelect;
	}
	private JScrollPane getScrollPane() 
	{
		if (scrollPane == null) 
		{
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 41, 426, 249);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() 
	{
		if (textArea == null) 
		{
			textArea = new JTextArea();
		}
		return textArea;
	}
}
